home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTFIRST.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  90 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btfirst.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btfirst - first btree key
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btfirst(btp)
  20.      btree_t *btp;
  21.  
  22. DESCRIPTION
  23.      The btfirst function positions the cursor of btree btp on the
  24.      first key in that btree.
  25.  
  26.      btfirst will fail if one or more of the following is true.
  27.  
  28.      [EINVAL]       btp is not a valid btree pointer.
  29.      [BTELOCK]      btp is not locked.
  30.      [BTENKEY]      btp is empty.
  31.      [BTENOPEN]     btp is not open.
  32.  
  33. SEE ALSO
  34.      btkeycnt, btlast, btnext, btprev.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, a value of 0 is returned.  Otherwise,
  38.      a value of -1 is returned, and errno set to indicate the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. int btfirst(btp)
  42. btree_t *btp;
  43. {
  44.     int terrno = 0;        /* tmp errno */
  45.  
  46.     /* validate arguments */
  47.     if (!bt_valid(btp)) {
  48.         errno = EINVAL;
  49.         return -1;
  50.     }
  51.  
  52.     /* check if not open */
  53.     if (!(btp->flags & BTOPEN)) {
  54.         errno = BTENOPEN;
  55.         return -1;
  56.     }
  57.  
  58.     /* check locks */
  59.     if (!(btp->flags & BTLOCKS)) {
  60.         errno = BTELOCK;
  61.         return -1;
  62.     }
  63.  
  64.     /* set cursor to first key */
  65.     btp->cbtpos.node = btp->bthdr.first;
  66.     btp->cbtpos.key = 1;
  67.  
  68.     /* check if tree is empty */
  69.     if (btp->cbtpos.node == NIL) {
  70.         btp->cbtpos.key = 0;
  71.         bt_ndinit(btp, btp->cbtnp);
  72.         errno = BTENKEY;
  73.         return -1;
  74.     }
  75.  
  76.     /* read current node */
  77.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  78.         BTEPRINT;
  79.         terrno = errno;
  80.         btp->cbtpos.node = NIL;
  81.         btp->cbtpos.key = 0;
  82.         bt_ndinit(btp, btp->cbtnp);
  83.         errno = terrno;
  84.         return -1;
  85.     }
  86.  
  87.     errno = 0;
  88.     return 0;
  89. }
  90.